package com.hsylient.servlet;

import com.hsylient.dao.Employ;
import com.hsylient.utils.DbUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.*;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "/employServlet", value = "/employ/list")
public class EmployServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print("	<html>                                               ");
        out.print("		<head>                                           ");
        out.print("			<title>员工信息</title>                      ");
        out.print("		</head>                                          ");
        out.print("		<body>                                           ");
        out.print("		<h3 align='center'>员工列表</h3>                 ");
        out.print("		<hr width='60%' color='red'>                   ");
        out.print(" 		<table border='1' align='center' width='50%'> ");
        out.print("				<tr align='center'>                      ");
        out.print("					<th>序号</th>                        ");
        out.print("					<th>员工编号</th>                    ");
        out.print("					<th>员工姓名</th>                    ");
        out.print("					<th>员工薪水</th>                    	");
        out.print("				</tr>                                    ");
        QueryRunner queryRunner = DbUtils.getQueryRunner();
        Map<String, String> map = new HashMap<>();
        map.put("employ_id", "employId");
        map.put("employ_name", "employName");
        map.put("gmt_create", "gmtCreate");
        map.put("gmt_modified", "gmtModified");
        BeanListHandler<Employ> handler = new BeanListHandler<>(Employ.class, new BasicRowProcessor(new BeanProcessor(map)));
        List<Employ> list = null;
        try {
            list = queryRunner.query("select id, employ_id, employ_name,salary, gmt_create, gmt_modified from employ", handler);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        list.forEach(item -> {
            out.print("	<tr align='center'>     ");
            out.print("	<th>"+item.getId()+"</th>      ");
            out.print("	<th>"+item.getEmployId()+"</th>       ");
            out.print("	<th>"+item.getEmployName()+"</th>       ");
            out.print("	<th>"+item.getSalary()+"</th>         ");
            out.print("	</tr>                    ");
        });
        out.print("			</table>                                     ");
        out.print("		</body>                                          ");
        out.print("	</html>                                              ");
    }

    public void destroy() {
    }
}